<template>
  <view>
    <web-view
      ref="webview"
      :webview-styles="webviewStyles"
      :src="src"
      @message="handleMessage"
      @onPostMessage="handlePostMessage"
    ></web-view>
  </view>
</template>

<script>
import { mapState } from "vuex";
import { h5URL } from "@/config";
export default {
  onShow: function () {
    // 设置屏幕常亮
    uni.setKeepScreenOn({
      keepScreenOn: true,
      success: function () {
        console.log("屏幕保持常亮设置成功");
      },
      fail: function (err) {
        console.error("屏幕保持常亮设置失败", err);
      },
    });
  },

  // 当离开页面时，可以关闭屏幕常亮
  onHide: function () {
    uni.setKeepScreenOn({
      keepScreenOn: false,
      success: function () {
        console.log("屏幕常亮设置已关闭");
      },
      fail: function (err) {
        console.error("关闭屏幕常亮设置失败", err);
      },
    });
  },

  onUnload() {
    uni.setKeepScreenOn({
      keepScreenOn: false,
    });
  },

  computed: {
    ...mapState(["openid", "unionid"]),
    src() {
      return `${h5URL}?&k=${new Date().getTime()}`;
    },
  },
  data() {
    return {
      webviewStyles: {
        progress: {
          color: "#FF3333",
        },
      },
      isShow: true,
    };
  },
  methods: {
    handleMessage(res) {
      console.log("接收到H5页面的消息:", res);
    },
    handlePostMessage(res) {
      console.log("接收到H5页面的消息:handlePostMessage", res);
    },
    beforeleave() {
      this.isShow = false; // 移除弹框
      uni.showModal({
        title: "确定要退出吗",
        success: (e) => {
          if (e.confirm) {
            uni.navigateBack({ delta: 1 });
          } else {
            // 点取消，重新显示弹框
            this.isShow = true;
          }
        },
      });
    },
  },
  onShareAppMessage() {
    return {
      title: "试点项目",
      imageUrl: "",
      path: "/pages/index/index",
    };
  },
  onShareTimeline() {
    return {
      title: "试点项目",
      imageUrl: "",
      path: "/pages/index/index",
    };
  },
};
</script>
